﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;


namespace _3DFileConverter
{

    public partial class Form1 : System.Windows.Forms.Form {

        float scaleX = 1;
        float scaleY = 1;
        float scaleZ = 1;
        float[] scaleByXYZ;

        float translateX = 0;
        float translateY = 0;
        float translateZ = 0;
        float[] trnsByXYZ;

        float totalArea = 0;
        float totalVolume = 0;

        public Form1() {
            InitializeComponent();
        }

        void scaleVertex(VertexProp vertex, float[] scaleData) {
                vertex.X *= scaleData[0];
                vertex.Y *= scaleData[1];
                vertex.Z *= scaleData[2];
        }

        void translateVertex(VertexProp vertex, float[] translateData)
        {
                vertex.X += translateData[0];
                vertex.Y += translateData[1];
                vertex.Z += translateData[2];
        }

        private void button1_Click(object sender, EventArgs e) {

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "Choose File";
            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter = "obj files (*.obj)|*.obj|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            ObjFigure objFigure = new ObjFigure();
            ObjToStlConverter convertFigure = new ObjToStlConverter();

            List<string[]> facets = new List<string[]>();

            List<string[]> options = new List<string[]>();

            StlWriter stlObjWriter = new StlWriter();

            VertexProp verteces;
            VertexProp vertecesNormals;
            VertexProp vertecesTextures;

            //checks scale values
            if (byXtext.Text != null && byXtext.Text != "") {

                scaleX = float.Parse(byXtext.Text);
            }

            if (byYtext.Text != null && byYtext.Text != "") {

                scaleY = float.Parse(byYtext.Text);
            }

            if (byZtext.Text != null && byZtext.Text != "") {

                scaleZ = float.Parse(byZtext.Text);  
            }

            //checks translate values
            if (trnsByXtext.Text != null && trnsByXtext.Text != "") {

                translateX = float.Parse(trnsByXtext.Text);
            }

            if (trnsByYtext.Text != null && trnsByYtext.Text != "") {

                translateY = float.Parse(trnsByYtext.Text);
            }

            if (trnsByZtext.Text != null && trnsByZtext.Text != "") {

                translateZ = float.Parse(trnsByZtext.Text);  
            }

            scaleByXYZ = new float[] { scaleX, scaleY, scaleZ };
            trnsByXYZ = new float[] { translateX, translateY, translateZ };

            if (openFileDialog.ShowDialog() == DialogResult.OK) {

                string filePath;
                float x, y, z;
                string line;

                filePath = openFileDialog.FileName;
                StreamReader file = new StreamReader(filePath);

                while ((line = file.ReadLine()) != null) {

                    string[] propList = line.Split(' ');

                        switch (propList[0]) {

                            case "v":
                                x = float.Parse(propList[1]);
                                y = float.Parse(propList[2]);
                                z = float.Parse(propList[3]);
                                verteces = new VertexProp(x, y, z);
                                scaleVertex(verteces, scaleByXYZ);
                                translateVertex(verteces, trnsByXYZ);
                                objFigure.Verteces.Add(verteces);
                                break;

                            case "vt":

                                if (propList.Count() == 2) {

                                    x = float.Parse(propList[1]);
                                    vertecesTextures = new VertexProp(x);
                                    objFigure.VertecesTextures.Add(vertecesTextures);
                                }
                                else if (propList.Count() == 3){

                                    x = float.Parse(propList[1]);
                                    y = float.Parse(propList[2]);
                                    vertecesTextures = new VertexProp(x, y);
                                    objFigure.VertecesTextures.Add(vertecesTextures);
                                }
                                else {

                                    x = float.Parse(propList[1]);
                                    y = float.Parse(propList[2]);
                                    z = float.Parse(propList[3]);
                                    vertecesTextures = new VertexProp(x, y, z);
                                    objFigure.VertecesTextures.Add(vertecesTextures);
                                }
                                break;

                            case "vn":
                                x = float.Parse(propList[1]);
                                y = float.Parse(propList[2]);
                                z = float.Parse(propList[3]);
                                vertecesNormals = new VertexProp(x, y, z);
                                objFigure.VertecesNormals.Add(vertecesNormals);
                                break;

                            case "f":
                                facets.Add(propList);
                                convertFigure.facetCalculation(propList);
                                break;
                        }
                }
                stlObjWriter.writeHeader(convertFigure.NumberOfFacets);
                
                foreach (string[] element in facets) {
                    var tuple = convertFigure.convertFromTo(objFigure, element, scaleByXYZ, trnsByXYZ);
                    totalArea += tuple.Item1;
                    totalVolume += tuple.Item2;
                }

                MessageBox.Show("The work is done", "", MessageBoxButtons.OK);
                areaText.Text = totalArea.ToString();
                volumeText.Text = totalVolume.ToString();
                convertFrom.Text = "";
                convertTo.Text = "";
                convertTo.Enabled = false;
                button1.Enabled = false;
            }
        }

        private void convertFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            convertTo.Enabled = true;
        }

        private void convertTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = true;
        }
    }
}
